using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public static class Util
{


    public static Vector2 AutoSize(Vector2 origion, Vector2 size)
    {
        float pix = origion.x / origion.y;
        float pix2 = size.x / size.y;

        if (pix > pix2)
        {
            size.y = size.x / pix; 
        }
        else
        {
            size.x = pix * size.y;
        }

        return size;
    }


    private static Color[] colors = {
       GetColor (208,240,104),
       GetColor (244,185,100),
       GetColor (94,198,253),
       GetColor (169,126,253),
       GetColor (246,140,253),
       GetColor (255,255,0),
       GetColor (0,255,0),
       GetColor (0,255,255),
    };
    public static Color GetColor(float x, float y, float z, float a = 255)
    {
        return new Color(x / 255f, y / 255f, z / 255f, a / 255f);
    }

    public static Color GetRandomColor()
    {
        int count = colors.Length;

        int index = UnityEngine.Random.Range(0, count);

        return colors[index];
    }


    public static void ToSetConcentPos(ScrollRect curScrollRect, Transform curItemPanelPrefb, Transform toTran)
    {
        Vector2 itemSize = curItemPanelPrefb.GetComponent<RectTransform>().sizeDelta;

        Rect viewRect = curScrollRect.GetComponent<RectTransform>().rect;
        Vector2 viewSize = Vector2.zero;
        viewSize.x = viewRect.width;
        viewSize.y = viewRect.height;
        viewSize.y = Math.Abs(viewSize.y);

        RectTransform concent = curScrollRect.content;
        LayoutGroup layoutGroup = concent.GetComponent<LayoutGroup>();

        Vector2 concentsrizedata = concent.sizeDelta;
        concentsrizedata.y = Math.Abs(concentsrizedata.y);
        concentsrizedata.x = Math.Abs(concentsrizedata.x);

        Vector3 pos = concent.localPosition;
        if (layoutGroup is HorizontalLayoutGroup)
        {
            pos.x = 0;
            if (concentsrizedata.x > viewSize.x)
            {
                Transform tran = toTran;
                if (tran == null) return;
                float y = Math.Abs(tran.localPosition.y) - itemSize.y / 2 - layoutGroup.padding.top;
                float x = Math.Abs(tran.localPosition.x) - itemSize.x / 2 - layoutGroup.padding.top;
                pos.x = x;

                if (x > (concentsrizedata.x - viewSize.x))
                {
                    pos.x = (concentsrizedata.x - viewSize.x);
                }
            }
        }
        else
        {
            pos.y = 0;
            if (concentsrizedata.y > viewSize.y)
            {
                Transform tran = toTran;
                if (tran == null) return;
                float y = Math.Abs(tran.localPosition.y) - itemSize.y / 2 - layoutGroup.padding.top;
                float x = Math.Abs(tran.localPosition.x) - itemSize.x / 2 - layoutGroup.padding.top;
                pos.y = y;

                if (y > (concentsrizedata.y - viewSize.y))
                {
                    pos.y = (concentsrizedata.y - viewSize.y);
                }
            }
        }
        concent.localPosition = pos;
    }


}
